package com.lzy.cloud.goods.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.cloud.goods.dtos.ConfigDto;
import com.lzy.cloud.goods.facade.ConfigFacade;
import com.lzy.cloud.goods.request.ConfigRequest;
import com.lzy.cloud.goods.response.ConfigResponse;
import com.lzy.cloud.goods.service.ConfigService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by WIN7 on 2018/6/11.
 */
@RestController
@RequestMapping("/config")
public class ConfigController {

    @Autowired
    ConfigService configService;

    @Autowired
    ConfigFacade configFacade;

    /**
     * 新增车内配置
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody(required = false) ConfigRequest request){
        Ensure.that(request).isNotNull("00010001");
        request.setMode(1);//平台端
        Boolean ok = configService.add(DozerUtils.map(request, ConfigDto.class))>0;
        Ensure.that(ok).isTrue("00060001");
        return new Result(ok);
    }

    /**
     * 删除车内配置
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/del/{ids}",method = RequestMethod.GET)
    public Result del(@PathVariable("ids") String ids){
        Boolean ok = configFacade.del(ids);
        Ensure.that(ok).isTrue("00060002");
        return new Result(ok);
    }

    /**
     * 编辑车内配置
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(@RequestBody(required = false) ConfigRequest request){
        Ensure.that(request).isNotNull("00010001");
        Boolean ok = configService.update(DozerUtils.map(request, ConfigDto.class));
        Ensure.that(ok).isTrue("00060003");
        return new Result(ok);
    }

    /**
     * 根据id查询车内配置
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail/{id}",method = RequestMethod.GET)
    public Result update(@PathVariable("id") Long id){
        ConfigDto configDto = configService.selectById(id);
        Ensure.that(configDto).isNotNull("00060004");
        return new Result(DozerUtils.map(configDto,ConfigResponse.class));
    }


    /**
     * 分页查询车内配置列表,ConfigRequest.mode代表平台和商家
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result list(ConfigRequest request){
        Ensure.that(request).isNotNull("00010001");
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo pageInfo = configService.selectList(pager,DozerUtils.map(request, ConfigDto.class));
        if (pageInfo != null && pageInfo.getList() != null) {
            pager.setTotal(pageInfo.getTotal());
            return new Result(pager,DozerUtils.maps(pageInfo.getList(), ConfigResponse.class));
        } else {
            return new Result();
        }
    }

    /**
     * 启用/禁用
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/status/{status}/{ids}",method = RequestMethod.GET)
    public Result status(@PathVariable("status") Integer status,@PathVariable("ids") String ids){
        Boolean ok = configFacade.updateStatus(status,ids);
        Ensure.that(ok).isTrue("00060005");
        return new Result(ok);
    }
}
